package com.tarena.lbs.content.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.content.dao.mapper.CategoryMapper;
import com.tarena.lbs.content.dao.repository.CategoryRepository;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CategoryRepositoryImpl implements CategoryRepository {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageInfo<ArticleCategoryPO> pageList(ArticleCategoryQuery articleCategoryQuery) {
        LambdaQueryWrapper<ArticleCategoryPO> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(articleCategoryQuery.getCategoryName())){
            queryWrapper.like(ArticleCategoryPO::getCategoryName,articleCategoryQuery.getCategoryName());
        }
        if(articleCategoryQuery.getCategoryStatus() != null){
            queryWrapper.eq(ArticleCategoryPO::getCategoryStatus,articleCategoryQuery.getCategoryStatus());
        }
        PageHelper.startPage(articleCategoryQuery.getPageNo(), articleCategoryQuery.getPageSize());
        List<ArticleCategoryPO> pageResult = categoryMapper.selectList(queryWrapper);
        return new PageInfo<>(pageResult);
    }
}
